import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ProviderDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Scaffold(
      appBar: AppBar(
        title: Text('ProviderDemo'),
        centerTitle: true,
      ),
      body: ChangeNotifierProvider(
        create: (context) => _MyMode(),
        child: Center(
          child: Consumer<_MyMode>(
            builder: (context, mode, child) {
              // mode = context.watch<_MyMode>()
              return Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(30.0),
                    child: Text('${context.watch<_MyMode>().count}'),
                  ),
                  FloatingActionButton(
                    onPressed: mode.add,
                    child: Icon(Icons.add),
                  )
                ],
              );
            },
          ),
        ),
      ),
    );
  }
}

class _MyMode extends ChangeNotifier {
  int count = 0;
  add() {
    count ++;
    notifyListeners();
  }
}